package com.moli.iov.task.jobhandler;

import com.moli.iov.alarm.remote.IAlarmQuantityStatRemoteService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * <p>
 *  Synchronized alarm quantity statistics job.
 * </p>
 *
 * @author OAK
 * @version 3.0
 * @since 2019/10/21 10:39 AM.
 */
@Slf4j
@JobHander(value="alarmQuantityStatisticsHandler")
@Component
public class AlarmQuantityStatisticsHandler extends IJobHandler {

    @Autowired
    IAlarmQuantityStatRemoteService alarmQuantityStatRemoteService;

    @Override
    public ReturnT<String> execute(String... param) throws Exception {
        try {
            log.info(" Execute synchronized alarm quantity statistics in parameter : {}", param);
            final String currentDate = DateUtil.format(DateUtil.YEAR_DAY,
                    new Date(Instant.now().minus(1, ChronoUnit.DAYS).toEpochMilli()));
            RestResponse<String> restResponse = alarmQuantityStatRemoteService.executeGenericAlarmQuantityStat(currentDate);
            log.info(" Execute synchronized alarm quantity statistics success.");
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            log.error(" Execute synchronized alarm quantity statistics found a fail, {}", e);
            return ReturnT.FAIL;
        }
    }
}
